﻿using Common.Utils;
using Entity.Models.dto;
using Entity.Models.Entities;
using Entity.Models.vo;
using IBLL;
using Microsoft.AspNetCore.Mvc;
using RepositorySys.Filte;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using utils;
using utils.enums;
using utils.EX;
using Utils;

namespace RepositorySys.Controllers
{
    [CustomActionFilte]
    public class CategoryController : Controller
    {
        //耗材分类接口
        ICategoryBLL _categoryBLL;

        //用户接口
        IUserBLL _userBLL;


        public CategoryController(ICategoryBLL categoryBLL, IUserBLL userBLL)
        {
            _categoryBLL = categoryBLL;
            _userBLL = userBLL;
        }

        #region 视图

        /// <summary>
        /// 耗材分类列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddCategoryView()
        {
            return View();
        }

        /// <summary>
        /// 修改视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdataCategoryView()
        {
            return View();
        }
        #endregion


        /// <summary>
        /// 耗材分类列表、搜索
        /// </summary>
        /// <param name="categoryName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getCategoryList(string categoryName, string page, string limit)
        {
            List<CategoryDTO> category = null;
            int cont = 0;
            try
            {
                category = _categoryBLL.getcategoryList(categoryName, page, limit, out cont);
            }
            catch (MyException e)
            {
                //获取异常信息
                return getException(category, e);
            }
            return Json(new JsonResults(0, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), cont, category));
        }

        /// <summary>
        /// 添加耗材分类
        /// </summary>
        /// <param name="category">耗材分类信息</param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult AddCategory(Category category)
        {
            //状态信息
            string mgs;
            object data = null;
            if (Verify(category, out mgs))
            {
                try
                {
                    data = _categoryBLL.AddCategory(category);
                }
                catch (MyException e)
                {
                    return getException(data, e);
                }
            }
            return Json(new JsonResults(200, string.IsNullOrEmpty(mgs) ? Enums.getDescribe(UserEnums.USER_STATE.SUCCESS) : mgs, data));
        }


        /// <summary>
        /// 删除耗材分类
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DalateCategory(string categoryId)
        {
            object data = null;
            try
            {
                data = _categoryBLL.DalateCategory(categoryId);
            }
            catch (MyException e)
            {
                return getException(data, e);
            }
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.DATATE), data));
        }



        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="categoryIds"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DalateCategoryList(List<string> categoryIds)
        {
            object data = null;
            try
            {
                data = _categoryBLL.DalateCategoryList(categoryIds);
            }
            catch (MyException e)
            {
                return getException(data, e);
            }
            return Json(new JsonResults(200, Enums.getDescribe(UserEnums.USER_STATE.DATATE), data));
        }

        /// <summary>
        /// 修改耗材分类信息
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public IActionResult UpdataCategory(Category category)
        {
            object data = null;
            string mgs = null;
            if (Verify(category, out mgs))
            {
                try
                {
                    data = _categoryBLL.UpdataCategory(category);
                }
                catch (MyException e)
                {
                    return getException(data, e);
                }
            }
            string a = string.IsNullOrEmpty(mgs) ? Enums.getDescribe(UserEnums.USER_STATE.UPDATE) : mgs;
            return Json(new JsonResults(200, a, data));
        }

        /// <summary>
        /// 根据耗材分类id搜索(修改页面数据列表)
        /// </summary>
        /// <param name="categoryid">耗材分类id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getByCategory(string categoryid)
        {
            return Json(new JsonResults(200, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), _categoryBLL.getByCategory(categoryid)));
        }

      

        /// <summary>
        /// 参数验证
        /// </summary>
        /// <param name="category">参数</param>
        /// <returns></returns>
        private bool Verify(Category category, out string mgs)
        {
            if (String.IsNullOrEmpty(category.CategoryName))
            {
                mgs = "耗材分类名称不能为空";
                return false;
            }
            //else if (category.LeaderId.EndsWith("0"))
            //{
            //    mgs = "主管不能为空";
            //    return false;
            //}
            //else if (String.IsNullOrEmpty(category.ParentId))
            //{
            //    mgs = "父耗材分类不能为空";
            //    return false;
            //}
            mgs = null;
            return true;
        }

        /// <summary>
        /// 获取异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private IActionResult getException(object data, MyException e)
        {
            var tt = e.GetExceptionResult();
            return Json(new JsonResults(tt.resultStatus, tt.resultMsg, data));
        }
    }
}
